Multi-threading is the partitioning of a computer program that performs a particular process into logically independent “threads” of control that can execute in parallel. Each thread includes a sequence of instructions and data used by the instructions to carry out a particular program task, such as a computation or input/output function.
Although a single processor can execute instructions of only one thread at a time, the processor can execute multiple threads in parallel by, for example, executing instructions corresponding to one thread until reaching a selected instruction, suspending execution of that thread, and executing instructions corresponding to another thread, until all threads have completed. In this scheme, as long as the processor has begun executing instructions for more than one thread during a given time interval all executing threads are said to be “running” during that time interval, and multi-processing is achieved.
A process includes multiple threads operating out of a common address space. Processes may be configured such that portions of their address space are shared, that is, are mapped to a common physical location, or a memory. Multiple threads within a process share portions of that memory address space. It is possible for more than one thread to believe it has sole access to a portion of memory and for it to perform a series of reads and writes to an area of memory while another thread is performing reads and writes to the same area of memory. Such concurrent operation by competing threads may confuse operation of a process. Therefore, in a shared memory environment the shared memory must be allocated to each thread based on a coordination technique ensuring that only one thread has access to an area of shared memory at a time.
Current memory allocation techniques assign address space to threads in one of two ways: distribution or mutual exclusion. Distribution techniques partition shared memory into equal-sized blocks and assign the memory blocks to threads as requested. Each memory block must be large enough to meet the memory requirements of the thread requiring the most memory. Total memory requirements are dictated by the maximum demand for access to shared memory of a given thread multiplied by the total number of threads involved. This method is particularly wasteful when individual threads do not require similar amounts of shared memory. Even slight differences in the threads' memory requirements can have a significant impact on the total memory requirement.
Mutual exclusion techniques, which may be implemented in hardware or software, coordinate use of shared memory among threads by reading and writing shared variables and pointers referring to the shared addresses. Implementation of these techniques requires accessing the operating system, which is both slow and expensive.
In a mutual exclusion implementation the operating system only permits one thread at a time to access shared memory. Shared memory is assigned to a thread by exchanging signals between threads, such that a thread can be forced to stop at a specified point during execution until it has received a specific signal. More specifically, the operating system may allow a thread to access shared memory by locking the shared memory, allocating the memory for exclusive use of the thread, and unlocking the memory once the thread has completed the memory operation.
While the memory area is locked other threads are prevented from accessing it until the lock has been removed. This method is an acceptable way of allocating memory for one-time or infrequent operations, for example, the one-time startup required by a process. However, computing generally requires execution of operations that occur hundreds or even thousands of times per second, for example, when many threads are modifying a common memory area by performing a “write” operation. For such operations this method of locking and unlocking shared memory creates bottlenecks in system performance. As the number of threads making demands on system performance increases, the likelihood of a system bottleneck increases. This of course negatively impacts system performance.